GBML manual / Strings

string_insert

text with part pushed in at position at (0-based) - string_insert("cat", "h", 1) is "chat". The result is longer than text, so if it does not fit in a string, the END of the result is quietly cut off: the last letters of text (and, if even that is not enough, the end of part) are the ones lost.

Syntax

string_insert(text, part, at) -> string

Arguments

NameType
textstring
partstring
atint

Returns

string

Description

text with part pushed in at position at: string_insert("cat", "h", 1) is "chat". The answer is longer than text, and a string only has so much room - so if it does not fit, the end of the answer is cut off (no error, and the game keeps running). The last letters of text go first, and if even that is not enough, the end of part goes too.

Positions count from 0, exactly like string_char_at: at of 0 puts part at the very front, and at equal to the length of text puts it at the very end. An at below 0 acts as 0, and an at past the end acts as the end. It never crashes.

Example

var word = "cat";
var chat = string_insert(word, "h", 1);        // "chat"
var front = string_insert(word, "the ", 0);    // "the cat"
var back = string_insert(word, "s", 3);        // "cats"
var clamped = string_insert(word, "!", 99);    // "cat!" - past the end means the end

When it does not fit

Say a string has room for 8 characters. string_insert("hello", "XYZXYZXYZ", 2) is built from the front: he, then XYZXYZXYZ, then llo. Only 8 characters fit, so you get "heXYZXYZ" - the llo is gone and so is the end of the XYZ run. In your own game the room is your project's Longest string setting (32 characters unless you changed it), which is usually plenty for a line of dialogue.

To find out whether anything was lost, compare the lengths: the answer should be string_length(text) + string_length(part) long, and is shorter if it was cut.

If you keep the answer in a variable that you gave a smaller room with /// @capacity, and the answer is longer than that room, the game stops with an error screen when the line runs (a build with the safety checks on, which is the default). The cutting happens inside string_insert, at the Longest string size - not at the size of the variable that receives the answer. See Text.

On the GBA

Counts from 0, like string_char_at: at = 0 puts part at the very front, at = string_length(text) puts it at the very end. An at below 0 acts as 0 and an at past the end acts as the end - never a crash. A string has a fixed amount of room (your project's Longest string setting, 32 characters unless you changed it). The result is built front to back and stops when the room is full, so what falls off is the end of the finished result: first the tail of text that would have come after part, and then the end of part too if part alone does not fit in what is left. Silent: no error, the game carries on. Compare string_length on the result with string_length(text) + string_length(part) to find out whether anything was lost.

See also: string_deletestring_joinstring_slice